// 01 场景
const scene = new THREE.Scene()

// 02 摄像机(透视相机)
const camera = new THREE.PerspectiveCamera(75,window.innerWidth/window.innerHeight,0.1,1000)

// 03 渲染器
const renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth,window.innerHeight);
// 把渲染器追加到页面中
document.body.appendChild(renderer.domElement);

// 04 创建一个立方体
// 4.1模型
const geometry =  new THREE.BoxGeometry();
// 4.2材质
const material =  new THREE.MeshBasicMaterial({color:0x3366ff})
const material2 = new THREE.MeshPhongMaterial({color:0x33ff66})
// 4.3立方体
const cube = new THREE.Mesh(geometry,material2)

// 4.4 立方体添加场景
scene.add(cube);


// 05 移动相机的位置
camera.position.z = 5;

// 08 添加灯
// 点光，颜色白色，强度100
const light1 = new THREE.PointLight(0xffffff,1,100);
scene.add(light1);//添加到场景
light1.position.set(10,10,10);//设置灯的位置
// 添加一盏环境灯
const light2 = new THREE.AmbientLight(0xffffff,0.4)
// 加入场景
scene.add(light2);
var list = [];

// 07 添加动画
function animate(){
  // 相当于setTimeout((),16)  
    cube.rotation.y +=0.01
    cube.rotation.x +=0.01
    for(var i=0;i<list.length;i++){
      list[i].rotation.y+= H  
      list[i].rotation.x+=Math.random()*0.01;
      
    }
    renderer.render(scene,camera)
    requestAnimationFrame(animate);
}
animate();

// 09 轨道控制
// 创建轨道控制

const controls = new THREE.OrbitControls(camera, renderer.domElement);
for(var i=0;i<9;i++){
  for(var j=0;j<9;j++){
    for(var k=0;k<9;k++){
      const material3 = new THREE.MeshPhongMaterial({color:Math.round(10000000*Math.random())})
      var cube2 = new THREE.Mesh(geometry,material3);
      
      cube2.position.x= i*3-13;
      cube2.position.y=j*3-10;
      cube2.position.z=k*3-40;
      scene.add(cube2);   
      list.push(cube2);
    }
  }

}